<?php

namespace App\Http\Controllers\Admin;

use App\Model\Permission;
use App\Model\Role;
use App\Model\User;
use App\Org\code\Code;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redis;

class LoginController extends Controller
{
    //登录页面
    public function login(){
        return view('admin.login');
    }

    //验证码
    public function code(){
       $code = new Code();
       return $code->make();
    }

    //处理用户登录的方法
    public function doLogin(Request $request){
        //1.接收用户提交的数据
        $input = $request->except('_token');

        //2.验证用户数据
        //dd($data);
        //定义验证规则
        $rule = [
            'username' => 'required|between:4,18',
            'password' => 'required|between:6,18|alpha_dash',
        ];
        //定义错误提升信息
        $msg = [
            'username.required' => '用户名不能为空',
            'username.between' => '用户名长度必须在4到18位之间',
            'password.required' => '密码不能为空',
            'password.between' => '密码长度必须在6到18位之间',
            'password.alpha_dash' => '密码必须为数字、字母或下划线',
        ];
        //Validator::make('需要验证的表单数据', '验证规则','错误提示信息');
        //进行表单验证
        $validator = Validator::make($request->all(), $rule,$msg);

        if ($validator->fails()) {
            return redirect('admin/login')
                ->withErrors($validator)
                ->withInput();
        }
        //验证验证码
        if(strtolower($input['code']) != strtolower(Session::get('code'))){
            return redirect('admin/login')->with("errors",'验证码错误！');
        }

        //3.验证是否有此用户
        $user = User::where('user_name','=',$input['username'])->first();
        if(!$user){
           return redirect('admin/login')->with("errors",'用户名错误！');
        }

        //验证密码是否正确
        if($input['password'] != Crypt::decrypt($user->user_pass)){
            return redirect('admin/login')->with("errors",'密码错误！');
        }

        //4.保存信息到session中
        Session::put('user',$user);
        Session::put('username',$user['user_name']);
        //将拥有的权限存入session
        Role::get();
        Permission::get();
        //获取当前用户拥有的角色
        $roles = $user->role;
        //存放所有权限
        $arr = [];
        //根据用户拥有的角色，找对应的权限
        foreach ($roles as $per){
            foreach ($per->permission as $v){
                $arr[] = $v->per_url;
            }
        }

        //去掉重复的权限
        $arr = array_unique($arr);
        //dd($arr);
        Session::put('permission',$arr);

        //5.跳转后台首页
        return redirect('admin/index');

    }

    //显示后台首页
    public function index(){
        return view('admin.index');
    }

    //显示后台欢迎页
    public function welcome(){
        return view('admin.welcome');
    }

    //退出登录
    public function loginout(){
        //清空session信息
        Session::flush();
        Redis::Flushall();
        //跳转登录页面
        return redirect('admin/login');
    }

    //没有权限对应的跳转
    public function noaccess(){
        return view("errors.noaccess");
    }



    public function encryption(){
        //md5加密
//        $md5_str = md5('123456');
//        $str = 'e10adc3949ba59abbe56e057f20f883e';
//        if($md5_str == $str){
//            return '密码正确！';
//        }

        //hash加密
//        $str = '123456';
//        $hash_str = Hash::make($str);
//        if(Hash::check($str,$hash_str)){
//            return '密码正确！';
//        }else{
//            return '密码错误！';
//        };

        //crypt加密
        $str = '123456';
        //return Crypt::encrypt($str);
        $crypt_str = 'eyJpdiI6ImhMeHR5WGFIV1A2NVZmcUgrWW5rRWc9PSIsInZhbHVlIjoiSWRhNWY1XC9JNnpkTlwvOENDQVZyMTVBPT0iLCJtYWMiOiIxZTM5NTY5NzI0ZGU4MmE3ODc4M2YyMDY3M2FhZGRjODIwMmZjMTEyYTc1ZjhiOTlhMDM2NTI5MTVlYjk2MmRhIn0';

        if(Crypt::decrypt($crypt_str) == $str){
            return '密码正确！';
        }else{
            return '密码错误！';
        }
    }
}
